﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoreWinform.ServiceReference1;
using StoreWinform.Model;
using BusinessCommon.Request;
using BusinessCommon.Responce;
using BusinessCommon.DTO;
using BusinessCommon.Utilities;
using System.Threading.Tasks;

namespace StoreWinform.Controller
{
    /// <summary>
    /// 
    /// </summary>
    public class LoginFormController
    {
        LoginServiceClient client = new LoginServiceClient("firstService");
        private static LoginFormController instance = new LoginFormController();
        private LoginFormModel model;
        private LoginForm FormView;
        public Action functionToCallLater;

        public LoginFormController()
        {
            this.FormView = GetView;
            this.model = GetModelView;
        }

        public Action GetFunctionToCallLater
        {
            get
            {
                return this.functionToCallLater;
            }
            set
            {
                functionToCallLater = value;
            }
        }

        public LoginForm GetView
        {
            get
            {
                if (this.FormView == null)
                {
                    this.FormView = new LoginForm();
                }
                return this.FormView;
            }
        }

        public LoginFormModel GetModelView
        {
            get
            {
                if (this.model == null)
                {
                    this.model = new LoginFormModel();
                }
                return this.model;
            }
        }

        public static LoginFormController Instance
        {
            get { return LoginFormController.instance; }
        }

        public void OpenLoginForm(LoginForm formView)
        {
            FormView = formView;
            FormView.SetController(this);
            FormView.Visible = true;
        }

        public void ClearInstance()
        {
            model = null;
            instance = null;
            FormView = null;
        }

        /// <summary>
        /// User login
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool UserLogin(String userName, String password)
        {
            UserRequest request = new UserRequest();
            request.UserName = userName;
            request.PasswordChange = StringUtilities.EncryptPassword(password);

            // Service: LoginAction
            UserResponce responce = client.LoginAction(request);

            if (responce.IsSuccess)
            {
                UserDto userDto = responce.userDto;
                if (userDto != null)
                {
                    SessionUtilites.SetSession(SessionUtilites.SESSION_KEY, userDto);
                    if (!StringUtilities.IsNull(responce.ErrorCode) && responce.ErrorCode.Equals(ItemDefineIF.ERR_00005))
                    {
                        SessionUtilites.SetSession(SessionUtilites.SESSION_KEY, userDto);
                        if (FormView.InvokeRequired)
                        {
                            FormView.BeginInvoke(new Action(() => FormView.ShowChangePasswordForm()));
                        }
                    }
                    else
                    {
                        return true;
                    }
                }
            }
            else
            {
                if (responce.ErrorCode != null)
                {
                    if (FormView.InvokeRequired)
                    {
                        FormView.BeginInvoke(new Action(() => FormView.ShowMessage(responce.ErrorCode, SmEnum.MSG_BOX_TYPE.ERR)));
                    }
                }
            }
            return false;
        }
    }
}
